<!--<style>-->
    <!--.layui-btn{margin:3px;}-->
<!--</style>-->

<div style="margin-left: 94%;">
    <a class="layui-btn layui-btn-primary layui-btn-sm operate" operate-type="add"><i class="layui-icon">&#xe608;</i>添加</a>
</div>

<table class="layui-table">
    <thead>
    <tr>
        <th width="5%;">ID</th>
        <th width="10%;">职位名称</th>
        <th width="10%;">描述</th>
        <th width="10%;">操作</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
</table>
<!--分页start-->
<div id="paginate"></div>
<!--分页end-->

<script>
    $(function(){
        GetTableList();
    });

    //获取表格数据
    function GetTableList(page=1,pagesize=10){
        var url = '/handler/user/getPositionList';
        var data = {page:page,pagesize:pagesize};
        dink.post(url,data,function(res){
            if(res.ret == 1){
                var target = $('tbody');
                var html = '';
                var tmp_root = 0;
                $.each(res.data,function(i,ele){
                    if(ele.name != '超级管理员'){
                        html += '<tr>\n' +
                            '    <td>'+(i+1)+'</td>\n' +
                            '    <td>'+ele.project_name+'---'+ele.name+'</td>\n' +
                            '    <td>'+ele.description+'</td>\n' +
                            '    <td>\n' +
                            '        <a class="layui-btn layui-btn-xs operate" operate-type="edit" data-id="'+ele.id+'">编辑</a>\n' +
                            '        <a class="layui-btn layui-btn-xs operate" operate-type="delete" data-id="'+ele.id+'">删除</a>\n' +
                            '        <a class="layui-btn layui-btn-xs operate" operate-type="power" data-id="'+ele.id+'">权限</a>\n' +
                            '    </td>\n' +
                            '</tr>';
                    }else{
                        tmp_root++;
                    }
                });
                if(html != ''){
                    if(page == 1){
                        Paginate(res.total - tmp_root,pagesize);
                    }
                }else{
                    html = '<tr><td colspan="6">无数据</td></tr>';
                }
                target.html(html);
            }else{
                var target = $('tbody');
                var html = '<tr><td colspan="6">无数据</td></tr>';
                target.html(html);
                layer.msg(res.msg);return;
            }
        });
    }

    //分页
    function Paginate(count,pagesize){
        layui.use('laypage',function(){
            var laypage = layui.laypage;
            laypage.render({
                elem: 'paginate',
                count: count,
                limit: pagesize,
                layout: ['count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj,first){
                    if(!first){
                        GetTableList(obj.curr,pagesize);
                    }
                }
            });
        });
    }

    //操作按钮click事件
    dink.click('.operate',function(){
        var operate_type = $(this).attr('operate-type');
        var id = $(this).attr('data-id');
        //判断权限
        dink.post('/handler/permission/checkOperatePermission',{permission:'user:position:'+operate_type},function(res){
            if(res.ret == 1){
                AdminUserPositionOpreate(operate_type,id);
            }else{
                layer.msg(res.msg);return;
            }
        });
        //end
    });

    //添加/编辑项目
    function AdminUserPositionOpreate(operate='add',id=0){
        if(operate == 'add'){
            dink.html('AdminUserPositionAdd','添加职位',['400px','250px']);
        }else if(operate == 'edit'){
            dink.html('AdminUserPositionAdd','编辑职位',['400px','250px']);
            setTimeout(function(){
                dink.post('/handler/user/getPositionDetailById',{id:id},function(res){
                    var item = res.data;
                    $('input[name=name]').val(item.name);
                    $('input[name=description]').val(item.description);
                });
            },50);
        }else if(operate == 'delete'){
            dink.alert('Tips','确定删除？',function(){
                dink.post('/handler/user/deletePosition',{id:id},function(res){
                    layer.msg(res.msg);
                    GetTableList();return;
                });
            });
        }else if(operate == 'power'){
            SetPower(id);return;
        }else{
            layer.msg('操作类型异常');return;
        }
        //添加、编辑
        dink.click('#add',function(){
            var data = {};
            var base = {};
            if(operate == 'add'){
                var url = '/handler/user/addPosition';
            }else if(operate == 'edit'){
                var url = '/handler/user/editPosition';
                if(id == 0){
                    layer.msg('缺少记录id');return;
                }
                data.id = id;
            }else{
                layer.msg('操作类型异常');return;
            }
            base.name = $('input[name=name]').val();
            base.description = $('input[name=description]').val();
            data.base = base;
            if(base.name == ''){
                layer.msg('请填写职位名称');return;
            }
            dink.alert('Tips','确定'+(operate=='add' ? '添加' : '编辑')+'职位？',function(){
                dink.post(url,data,function(res){
                    if(res.ret == 1){
                        dink.close(res.msg);
                        GetTableList();
                    }else{
                        layer.msg(res.msg);
                    }
                });
            });
        });
    }

    //设置权限
    function SetPower(role_id){
        dink.html('AdminPermissionProjectPowerSet','设置权限',['100%','100%']);
        //判断是否已设置，是就回填
        setTimeout(function(){
            dink.post('/handler/permission/getPowersByRoleId',{role_id:role_id},function(res){
                var powerArr = res.data.power_ids.split(',');
                $.each(powerArr,function(i,ele){
                    $('input[type=checkbox][value='+ele+']').prop('checked',true);
                });
            });
        },200);
        dink.click('#power_set',function(){
            var power_ids = '';
            $.each($('input[type=checkbox]:checked'),function(i){
                if(i == 0){
                    power_ids += $(this).val();
                }else{
                    power_ids += ',' + $(this).val();
                }
            });
            var data = {};
            var base = {};
            var url = '/handler/permission/setProjectPower';
            base.role_id = role_id;
            base.power_ids = power_ids;
            data.base = base;
            if(base.power_ids == ''){
                layer.msg('未选择任何权限');return;
            }
            dink.alert('Tips','确定设置？',function(){
                dink.post(url,data,function(res){
                    if(res.ret == 1){
                        dink.close(res.msg);
                        GetTableList();
                    }else{
                        layer.msg(res.msg);
                    }
                });
            });
        });
    }
</script>